// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import 'babel-polyfill'

Vue.config.productionTip = false
Vue.use(ElementUI)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

Vue.prototype.setCookie = (name, value, days) => {

    var d = new Date;

    d.setTime(d.getTime() + 24*60*60*1000*days);

    window.document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();

}

Vue.prototype.getCookie = (name) => {

    var v = window.document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');

    return v ? v[2] : null;

}

Vue.prototype.clearCookie = (name) => {

    this.setCookie(name, '', -1);

}

// 输入框的一些验证方法
// 价格输入
// 输入框价格输入限制 两位小数
Vue.prototype.onkeyupPrice = (e) => {
    var val = e.target.value
    //得到第一个字符是否为负号
    var t = val.charAt(0);
    //先把非数字的都替换掉，除了数字和.     
    val = val.replace(/[^\d\.]/g, '');
    //必须保证第一个为数字而不是.       
    val = val.replace(/^\./g, '');
    //保证只有出现一个.而没有多个.       
    val = val.replace(/\.{2,}/g, '.');
    //保证.只出现一次，而不能出现两次以上       
    val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
    val = val.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
    //如果第一位是负号，则允许添加    
    // if (t == '-') {
    //     return;
    // }
    e.target.value = val;
}
// 正整数
Vue.prototype.onkeyupPlusInt = (e) => {
    var val = e.target.value
    if (val.length == 1) {
        val = val.replace(/[^1-9]/g,'')
    } else {
        val = val.replace(/\D/g,'')
    }
    if(val == 0) { val = '' };
    e.target.value = val;
}

// el-cascader 扩展 - 通过选中的 val 值获取其 label 值
Vue.prototype.getCascaderObj = function(val, opt){
    return val.map(function (value, index, array) {
        for (var itm of opt) {
            if (itm.value == value) { opt = itm.children; return itm; }
        }
        return null;
    });
}